#ifdef _GT4
 // A1ContentHandler.cpp: derived class from SAXContentHandlerImpl
#include <vcl.h>
#pragma hdrstop

#include <eic.h>
#include "GT4_ContentHandler.h"
#include "GT4.h"

//#define MODULE_TAG 150
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


GT4_ContentHandler::GT4_ContentHandler(cModeleSpecifique *pModeleSpecifique, const AnsiString &Fichier)
{
	idnt = 0;
  m_ModeleSpecifique = pModeleSpecifique;
  m_Fichier = Fichier;
  m_Balises = new TStringList();
}

GT4_ContentHandler::~GT4_ContentHandler()
{
	//object destruction is handled by the Release() impl of parent class
  delete m_Balises;
}

//hides the new operator
GT4_ContentHandler * GT4_ContentHandler::CreateInstance(cModeleSpecifique *pModeleSpecifique, const AnsiString &Fichier)
{
	GT4_ContentHandler * pGT4_ContentHandler = NULL;
	pGT4_ContentHandler = new GT4_ContentHandler(pModeleSpecifique, Fichier);
	ASSERT(pGT4_ContentHandler!=NULL);
	return pGT4_ContentHandler;
}

HRESULT STDMETHODCALLTYPE GT4_ContentHandler::startElement(
            /* [in] */ const wchar_t __RPC_FAR *pwchNamespaceUri,
            /* [in] */ int cchNamespaceUri,
            /* [in] */ const wchar_t __RPC_FAR *pwchLocalName,
            /* [in] */ int cchLocalName,
            /* [in] */ const wchar_t __RPC_FAR *pwchRawName,
            /* [in] */ int cchRawName,
            /* [in] */ Msxml2_tlb::ISAXAttributes __RPC_FAR *pAttributes)
{
  AnsiString Balise(pwchLocalName);

  if( Balise.AnsiCompareIC( "AccountingEntryVoucher") == 0)
  {
    m_ModeleSpecifique->GdLivre.RAZ();
  }
  m_Balises->Add( AnsiString(pwchLocalName));


/*	prt(L"<%s", pwchLocalName, cchLocalName);
	int lAttr;
	pAttributes->getLength(&lAttr);
	for(int i=0; i<lAttr; i++)
	{
		const wchar_t * ln, * vl; int lnl, vll;
		pAttributes->getQName(i,&ln,&lnl);
		prt(L" %s=", ln, lnl);
		pAttributes->getValue(i,&vl,&vll);
		prt(L"\"%s\"", vl, vll);
	}
	prt(L">");   */

  return S_OK;
	// return E_FAIL;     //The parse operation should be aborted.
}

HRESULT STDMETHODCALLTYPE GT4_ContentHandler::endElement(
            /* [in] */ const wchar_t __RPC_FAR *pwchNamespaceUri,
            /* [in] */ int cchNamespaceUri,
            /* [in] */ const wchar_t __RPC_FAR *pwchLocalName,
            /* [in] */ int cchLocalName,
            /* [in] */ const wchar_t __RPC_FAR *pwchRawName,
            /* [in] */ int cchRawName)
{
  AnsiString Balise(pwchLocalName);
  try{
    AnsiString LastBalise = m_Balises->Count? m_Balises->Strings[m_Balises->Count-1] : EmptyStr;
    if( Balise.AnsiCompareIC( AccountingEntryVoucher) == 0){    m_ModeleSpecifique->GdLivre.RAZ();
    }else if( Balise.AnsiCompareIC( AccountingEntry) == 0){
    }else if( Balise.AnsiCompareIC( AccountingLine) == 0){      m_ModeleSpecifique->GdLivre.Libelle = EmptyStr;
    }else if( Balise.AnsiCompareIC( AccountingAccount) == 0){
                                              dmMI->InsereLigneTmpGdLivre( m_ModeleSpecifique->GdLivre, *m_ModeleSpecifique->Fichier);
                                              m_ModeleSpecifique->GdLivre.NoCompte = EmptyStr;
                                              m_ModeleSpecifique->GdLivre.Credit = 0;
                                              m_ModeleSpecifique->GdLivre.Debit = 0;
    }else if( Balise.AnsiCompareIC( MonetaryValue) == 0){
                                              switch( m_cSens)
                                              {
                                                case 'C' : m_ModeleSpecifique->LireMontant(m_Montant,m_ModeleSpecifique->GdLivre.Credit);
                                                           m_ModeleSpecifique->GdLivre.LettrageCredit = m_Lettrage;
                                                break;
                                                case 'D' : m_ModeleSpecifique->LireMontant(m_Montant,m_ModeleSpecifique->GdLivre.Debit);
                                                           m_ModeleSpecifique->GdLivre.LettrageDebit = m_Lettrage;
                                                break;
                                                default  : throw EUser( TAGL, AnsiString("Sens non reconnu '") + m_cSens + "'");
                                              }
                                              m_cSens = '\0';
                                              m_Montant = 0;
    }
    ASSERT( LastBalise == Balise);
    m_Balises->Delete( m_Balises->Count-1);
  }catch (EAbort &e)
  {
    return E_FAIL;
  }
  catch (Exception &e)
  {
    EICDisplayException(TAGL,e);
    return E_FAIL;
  }
  return S_OK;
	// return E_FAIL;     //The parse operation should be aborted.

}

HRESULT STDMETHODCALLTYPE GT4_ContentHandler::characters(
            /* [in] */ const wchar_t __RPC_FAR *pwchChars,
            /* [in] */ int cchChars)
{
  AnsiString s( WideString( pwchChars, cchChars)); s = s.Trim();
  if( (s != "\n") || (s != "\r\n")){
    try{
      AnsiString LastBalise = m_Balises->Count? m_Balises->Strings[m_Balises->Count-1] : EmptyStr,
                 BeforeLastBalise = m_Balises->Count-1>0? m_Balises->Strings[m_Balises->Count-2] : EmptyStr;
      if( LastBalise.AnsiCompareIC( AccountingEntryVoucher) == 0){
      }else if( LastBalise.AnsiCompareIC( Identification) == 0){
            if( BeforeLastBalise.AnsiCompareIC( AccountingEntryVoucher) == 0){
            }else if( BeforeLastBalise.AnsiCompareIC( AccountingEntry) == 0) {   m_ModeleSpecifique->GdLivre.NoPiece = s;
            }else if( BeforeLastBalise.AnsiCompareIC( AccountingAccount) == 0){  m_ModeleSpecifique->GdLivre.NoCompte = s;
            }
      }else if( LastBalise.AnsiCompareIC( Issue) == 0){   //date de la pièce comptable
      }else if( LastBalise.AnsiCompareIC( DepartmentIdentification) == 0){  //service à l'origine de la pièce comptable
      }else if( LastBalise.AnsiCompareIC( Status) == 0){                    //statut de l'écriture comptable
      }else if( LastBalise.AnsiCompareIC( Journal) == 0){                   m_ModeleSpecifique->GdLivre.CodeJournal = s;
      }else if( LastBalise.AnsiCompareIC( Value) == 0){                     m_ModeleSpecifique->GdLivre.DateImputation = TDateTime( s.SubString( 1, 4).ToInt(), s.SubString( 6, 2).ToInt(), s.SubString( 9, 2).ToInt());
      }else if( LastBalise.AnsiCompareIC( AccountingLine) == 0){
      }else if( LastBalise.AnsiCompareIC( Comment) == 0){                   m_ModeleSpecifique->GdLivre.Libelle = s;
      }else if( LastBalise.AnsiCompareIC( BookIdentification) == 0){
      }else if( LastBalise.AnsiCompareIC( Folio) == 0){
      }else if( LastBalise.AnsiCompareIC( Line) == 0){
      }else if( LastBalise.AnsiCompareIC( AccountingAccount) == 0){
      }else if( LastBalise.AnsiCompareIC( Local) == 0){                     m_Montant = s;
      }else if( LastBalise.AnsiCompareIC( DebitCredit) == 0){               m_cSens = s[1];
      }else if( LastBalise.AnsiCompareIC( MatchingIdentification) == 0){    m_Lettrage = s;
      }
    }catch (EAbort &e)
    {
      return E_FAIL;
    }
    catch (Exception &e)
    {
      EICDisplayException(TAGL,e);
      return E_FAIL;
    }
  }
  return S_OK;
	// return E_FAIL;     //The parse operation should be aborted.

}

#endif //_GT4
